package com.qf.spring002.factoryBean;

import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;
/**
 * 获取数据库连接对象的 FactoryBean
 */
public class ConnFactoryBean implements FactoryBean<Connection> {
    private String url;
    private String username;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * 该方法决定 bean 是不是单例的
     * false，代表bean不是单例的
     * true，代表bean是单例
     * @return
     */
    @Override
    public boolean isSingleton() {
        return false;
    }
//该方法决定了创建bean对象的具体过程
    @Override
    public Connection getObject() throws Exception {
        // 1. 加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");

        // 2. 获取数据库连接对象
        final Connection connection = DriverManager.getConnection(url, username, password);

        return connection;
    }
// 该方法决定返回对象的类型
    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
}
